#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>



typedef struct ListNode ListNode;
struct ListNode
{
    int val;
    ListNode* next;
};

ListNode* ListBuyNode(int x) {
    ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    if (node == NULL) {
        perror("malloc fail!");
        exit(1);
    }
    node->val = x;
    node->next = NULL;
    return node;
}
//创建带环链表
ListNode* CreateList(int n) {
    ListNode* phead = ListBuyNode(1);
    ListNode* ptail = phead;
    for (int i = 2; i <= n; i++) {
        ListNode* node = ListBuyNode(i);
        ptail->next = node;
        ptail = ptail->next;
    }
    ptail->next = phead;
    return ptail;
}

//n = 5 m = 2;
int ysf(int n, int m) {
    ListNode* prev = CreateList(n);
    ListNode* cur = prev->next;
    int count = 1;
    while (cur->next != cur) {
        if (count == m) {
            //杀掉
            prev->next = cur->next;
            free(cur);
            cur = prev->next;
            count = 1;
        }
        else {
            prev = cur;
            cur = cur->next;
            count++;
        }
    }
    return cur->val;
}